//
//  WZSafeArray.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/4/5.
//  Copyright © 2016年 wanzhao. All rights reserved.
//



#import "WZSafeArray.h"

static const void * const WZSafeArrayDispatchQueueSpecificKey = &WZSafeArrayDispatchQueueSpecificKey;

@interface WZSafeArray ()

{
    dispatch_queue_t    _queue;
}

@end

@implementation WZSafeArray

- (instancetype)init
{
    self = [super init];
    if (self) {
        _queue = dispatch_queue_create([[NSString stringWithFormat:@"fmdb.%@", self] UTF8String], NULL);
        dispatch_queue_set_specific(_queue, WZSafeArrayDispatchQueueSpecificKey, (__bridge void *)self, NULL);
        self.safeArray = [NSMutableArray array];
    }
    return self;
}

- (void)addObject:(id)object
{
    dispatch_block_t block = ^{
        if (object) {
            [self.safeArray addObject:object];
        }

    };
    
    if (dispatch_get_specific(WZSafeArrayDispatchQueueSpecificKey)) {
        block();
    } else {
        dispatch_async(_queue, block);
    }

}


- (void)removeObect:(id)object
{
    dispatch_block_t block = ^{
        if (object) {
            [self.safeArray removeObject:object];
        }
        
    };
    
    if (dispatch_get_specific(WZSafeArrayDispatchQueueSpecificKey)) {
        block();
    } else {
        dispatch_async(_queue, block);
    }
}

- (void)addObjectsFromArray:(NSArray *)otherArray
{
    dispatch_block_t block = ^{
        if (otherArray && otherArray.count > 0) {
            [self.safeArray addObjectsFromArray:otherArray];
        }
        
    };
    
    if (dispatch_get_specific(WZSafeArrayDispatchQueueSpecificKey)) {
        block();
    } else {
        dispatch_async(_queue, block);
    }
}

- (void)removeAllObjects
{
    dispatch_block_t block = ^{
        [self.safeArray removeAllObjects];
    };
    
    if (dispatch_get_specific(WZSafeArrayDispatchQueueSpecificKey)) {
        block();
    } else {
        dispatch_async(_queue, block);
    }
}

- (void)insertObject:(id)anObject atIndex:(NSUInteger)index
{
    dispatch_block_t block = ^{
        if (anObject) {
            [self.safeArray insertObject:anObject atIndex:index];
        }

    };
    
    if (dispatch_get_specific(WZSafeArrayDispatchQueueSpecificKey)) {
        block();
    } else {
        dispatch_async(_queue, block);
    }
}
- (void)removeObjectAtIndex:(NSUInteger)index
{
    dispatch_block_t block = ^{
        if (self.safeArray.count > index + 1) {
            [self.safeArray removeObjectAtIndex:index];
        }
        
    };
    
    if (dispatch_get_specific(WZSafeArrayDispatchQueueSpecificKey)) {
        block();
    } else {
        dispatch_async(_queue, block);
    }

}

- (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block
{
    dispatch_block_t ablock = ^{
        [self.safeArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            block(obj,idx,stop);
        }];
    };
    
    if (dispatch_get_specific(WZSafeArrayDispatchQueueSpecificKey)) {
        ablock();
    } else {
        dispatch_async(_queue, ablock);
    }

}
@end
